package com.forum.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.forum.common.utils.R;
import com.forum.user.entity.ForumUser;
import com.forum.user.entity.ForumUserFollow;
import com.forum.user.mapper.ForumUserFollowMapper;
import com.forum.user.mapper.ForumUserMapper;
import com.forum.user.service.ForumUserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@Service
public class ForumUserFollowServiceImpl extends ServiceImpl<ForumUserFollowMapper, ForumUserFollow> implements ForumUserFollowService {

    @Autowired
    private ForumUserFollowMapper forumUserFollowMapper;
    @Autowired
    private ForumUserMapper forumUserMapper;
    @Override
    public R findFollowUserByName(Long id,String nickName) {
        /**
         * 1、查出该用户所有的关注对象 list
         * 2、根据List模糊查询查到对应的id
         * 3、根据id批量查找
         */
        //1、查出该用户所有的关注对象 userList
        QueryWrapper<ForumUserFollow> forumUserFollowQueryWrapper = new QueryWrapper<>();
        forumUserFollowQueryWrapper.eq("id",id);
        Page<ForumUserFollow>page=new Page<>(1,10);
        List<ForumUserFollow> records = forumUserFollowMapper.selectPage(page, forumUserFollowQueryWrapper).getRecords();
        List<ForumUser>userList=new ArrayList<>();
        ForumUser forumUser=new ForumUser();
        for (ForumUserFollow record : records) {
            forumUser = forumUserMapper.selectById(record.getFollowId());
            userList.add(forumUser);
        }
        //2、根据List模糊查询查到对应的id
        List<Long>resultId=new ArrayList<>();
        Pattern pattern =Pattern.compile(nickName);
        for (int i=0;i<userList.size();i++){
            Matcher matcher=pattern.matcher(userList.get(i).getUserNickname());
            if (matcher.find()){
                resultId.add(userList.get(i).getId());
            }
        }
        //3、根据id批量查找

        List<ForumUser> forumUserList = forumUserMapper.selectBatchIds(resultId);


        return R.ok().put("success",true).put("forumUserList",forumUserList);
    }

    /**
     * 列出所有的关注用户
     * @param id 该用户的id
     * @return
     */
    @Override
    public R listAllFollowUser(Long id) {
        /**
         * 1、通过该用户的id查找followId
         * 2、根据followId查询关注的用户的信息
         */
        //1、通过该用户的id查找followId
        QueryWrapper<ForumUserFollow> forumUserFollowQueryWrapper = new QueryWrapper<>();
        forumUserFollowQueryWrapper.eq("id",id);
        Page<ForumUserFollow>page=new Page<>(1,10);
        List<ForumUserFollow> records = forumUserFollowMapper.selectPage(page, forumUserFollowQueryWrapper).getRecords();
       //2、根据followId查询关注的用户的信息
        List<ForumUser>userList=new ArrayList<>();
        ForumUser forumUser=new ForumUser();
        for (ForumUserFollow record : records) {
             forumUser = forumUserMapper.selectById(record.getFollowId());
            userList.add(forumUser);
        }
        return R.ok().put("success",true).put("followUserList",userList);
    }

    /**
     * 添加关注用户
     *
     * @param id 自己用户id
     * @param forumUser 关注的用户的用户名
     * @return
     */
    @Override
    public R addUserFollow(Long id,ForumUser forumUser) {
        /**
         *
         */
        ForumUserFollow forumUserFollow = new ForumUserFollow();
        forumUserFollow.setId(id);
        forumUserFollow.setFollowId(forumUser.getId());
        forumUserFollowMapper.insert(forumUserFollow);
        return R.ok().put("success",true);
    }

    /**
     * 删除关注用户
     * @param id 登录用户的id
     * @param forumUser 删除的目标用户的信息
     * @return
     */
    @Override
    public R removeUserFollow(Long id, ForumUser forumUser) {
        QueryWrapper<ForumUserFollow>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("follow_Id",forumUser.getId());
        forumUserFollowMapper.delete(queryWrapper);

        return R.ok().put("success",true);
    }



}
